package com.jdk.demo.fileChannel.read;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test2_1 {
	
	// 验证read返回值的意义
	public static void main(String[] args) throws Exception {
		FileInputStream fisRef = new FileInputStream("b.txt");
		FileChannel fileChannel = fisRef.getChannel();
		ByteBuffer bytebuffer = ByteBuffer.allocate(5);
		
		int readLength = fileChannel.read(bytebuffer);
		System.out.println(readLength); // 取得5个字节
		readLength = fileChannel.read(bytebuffer);
		System.out.println(readLength); // 取得0个字节，因为bytebuffer的remaining没有空间了
		bytebuffer.clear();
		readLength = fileChannel.read(bytebuffer);
		System.out.println(readLength); // -1，以为已经到达流的末尾
		
		bytebuffer.clear();
		fileChannel.close();
		fisRef.close();
	}

}
